<template>
    <template v-if="visible">
        <Teleport to="body">
            <div class="wheel-dialog-overlay" @click="onClickOverlay"></div>
            <div class="wheel-dialog-wrapper">
                <div class="wheel-dialog">
                    <header>
                        <slot name="title"/> 
                        <span @click="close" class="wheel-dialog-close"></span>
                    </header>
                    <main>
                        <slot name="content"/>
                    </main>
                    <footer>
                        <Button @click="onClickOk" level="main">OK</Button>
                        <Button @click="onClickCancel">Cancel</Button>
                    </footer>
                </div>
            </div>
        </Teleport>
    </template>
</template>

<script lang="ts">

    import Button from './Button.vue'

    export default {
        props: {
            visible: {
                type: Boolean,
                default: false
            },
            closeOnClickOverlay: {
                type: Boolean,
                default: true
            },
            ok: {
                type: Function
            },
            cancel: {
                type: Function
            }
        },
        components: { Button },
        setup(props, context) {
            const close = () => {
                context.emit('update:visible', false);
            };
            const onClickOverlay =() => {
                if(props.closeOnClickOverlay) {
                    close()
                }
            };
            const onClickOk = () => {
                if(props.ok && props.ok() !== false) {
                    close()
                }
            };
            const onClickCancel = () => {
                props.cancel && props.cancel();
                close();
            };
            return { close, onClickOverlay, onClickOk, onClickCancel }
        }
    }
</script>

<style lang="scss">
    $radius: 4px;
    $border-color: #D9D9D9;

    .wheel-dialog {
        min-width: 15em;
        max-height: 90%;
        border-radius: $radius;
        background: #FFF;
        box-shadow: 0 0 3px fade_out(#000, .5);

        &-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: fade_out(#000, .5);
            z-index: 10;
        }

        &-wrapper {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: 11;
        }

        > header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 12px 16px;
            font-size: 20px;
        }

        > main {
            padding: 12px 16px;
        }

        > footer {
            padding: 12px 16px;
            text-align: right;
        }

        &-close {
            position: relative;
            display: inline-block;
            width: 16px;
            height: 16px;
            cursor: pointer;

            &::before,
            &::after {
                content: '';
                position: absolute;
                top: 50%;
                left: 50%;
                width: 100%;
                height: 1px;
                background: #000;
            }

            &::before {
                transform: translate(-50%, -50%) rotate(-45deg);
            }

            &::after {
                transform: translate(-50%, -50%) rotate(45deg);
            }
        }
    }
</style>